package br.com.JZSolution.domineduplex.gui;

import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.net.BindException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import br.com.JZSolution.domineduplex.DomineDuplex;
import br.com.JZSolution.domineduplex.gui.dialogos.EnvioDialogo;
import br.com.JZSolution.domineduplex.gui.images.LeitorDeImagem;
import br.com.JZSolution.domineduplex.gui.util.ComunicadorDeMensagens;
import br.com.JZSolution.domineduplex.rede.ControladorDeServidor;

public class PainelDeBotoes extends JPanel {
	
	private JButton butConectar;
	private JButton butAbrir;
	private JButton butEnviar;
	private ImageIcon iconDesconectado = LeitorDeImagem.getInstance().carregarImagem("Desconectado.png");  
	private Cursor link = new Cursor(Cursor.HAND_CURSOR);
	private Cursor normal = new Cursor(Cursor.DEFAULT_CURSOR);
	private Dimension tamanhoBtn = new Dimension(50,50);
	
	public PainelDeBotoes(){
		super(new FlowLayout(FlowLayout.LEFT,0,0));
		criarBtnConectar();
		criarBtnAbrirPasta();
		criarBtnEnviar();
	}
	
	public void criarBtnEnviar(){
		butEnviar = new JButton(LeitorDeImagem.getInstance().carregarImagem("send.png"));
		butEnviar.setPreferredSize(tamanhoBtn);
		butEnviar.setToolTipText("Enviar arquivo");
		butEnviar.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				new EnvioDialogo();
			}
		});
		this.add(butEnviar);
	}
	
	public void criarBtnAbrirPasta(){
		butAbrir = new JButton(LeitorDeImagem.getInstance().carregarImagem("pasta.png"));
		butAbrir.setPreferredSize(tamanhoBtn);
		butAbrir.setToolTipText("Abrir pasta de downloads");
		butAbrir.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Desktop desk = Desktop.getDesktop();
				try {
					desk.open(new File(DomineDuplex.getInstance().getDiretorioDeRecepcao()));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		this.add(butAbrir);
	}
	
	public void criarBtnConectar() {
		butConectar = new JButton(iconDesconectado);
		butConectar.setPreferredSize(tamanhoBtn);
		butConectar.setToolTipText("Conectar");
		butConectar.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				DomineDuplex.getInstance().conectarDesconectar();
			}
		});
		
		butConectar.addMouseListener(new MouseListener(){
			public void mouseClicked(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {
				setCursor(link);
			}
			public void mouseExited(MouseEvent e) {
				setCursor(normal);
			}
			public void mousePressed(MouseEvent e) {}
			public void mouseReleased(MouseEvent e) {}
		});
		this.add(butConectar);
	}

	public JButton getButConectar() {
		return butConectar;
	}
}
